//
//  ConfriusernameViewController.m
//  project
//
//  Created by 张亚林 on 2017/10/25.
//  Copyright © 2017年 Macmini. All rights reserved.
//

#import "ConfriusernameViewController.h"

#import "CLTabBarViewController.h"
#import "ZWTools.h"
#import "MBProgressHUD+KR.h"

@interface ConfriusernameViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nicknameTF;


@end

@implementation ConfriusernameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)clickDone:(id)sender {
    
    NSInteger id = self.userid;
    NSString *nickname = self.nicknameTF.text;
    NSLog(@"%ld", id);
    NSLog(@"%@", nickname);

    NSString *post = [NSString stringWithFormat:@"nickName=%@", nickname];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://116.62.120.179:82/uc/editUserNickName"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:token forHTTPHeaderField:@"token"];
    [request setHTTPBody:postData];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSLog(@"%@", returnData);
    
    NSString *receiveStr = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
    NSData * data = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"%@", jsonDict);
   
    NSInteger result = [[jsonDict objectForKey:@"code"] integerValue];
    if (result == 0) {
        
        ZWUserModel *data2 = [NSKeyedUnarchiver unarchiveObjectWithFile:PATH_USERINFO_DATA];
        data2.nickName = self.nicknameTF.text;
        loginuserInfo = data2;
        [NSKeyedArchiver archiveRootObject:loginuserInfo toFile:PATH_USERINFO_DATA];
        
        is_nickname = YES;
         [MBProgressHUD showSuccess:@"设置昵称成功"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
               CLTabBarViewController *clTVC = [[CLTabBarViewController alloc] init];
               [UIApplication sharedApplication].keyWindow.rootViewController = clTVC;
            
        });
    } else if (result == 100) {
       
        [MBProgressHUD showSuccess:@"设置昵称失败"];
    }

    
}


@end
